package QianFeng06.ComparableDemo;

import java.util.Arrays;

/**
 * Comparable接口
 * 此接口强行对实现它的每个类的对象进行整体排序。
 * Comparator接口
 * 强行对某个对象 collection 进行整体排序的比较函数
 **/
public class Test {
    public static void main(String[] args) {
        int[] numbers = {34, 54, 32, 89, 19, 3};
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers));

        String[] names = {"jack", "tom", "涛宝", "大神"};
        Arrays.sort(names);
        System.out.println(Arrays.toString(names));

        //自定义对象，要实现比较排序在对象类中重定义compareTo
        Cat[] cats = {new Cat("大神", 3), new Cat("涛宝", 1)};
        Arrays.sort(cats);//sort不知道用什么排序，会报错Cat cannot be cast to java.lang.Comparable
        System.out.println(Arrays.toString(cats));

        //compare(T o1, T o2) 比较用来排序的两个参数。
        Dog[] dogs = {new Dog("大神", 3), new Dog("涛宝", 1)};
        Arrays.sort(dogs, new DogComparator());//sort不知道用什么排序，会报错Cat cannot be cast to java.lang.Comparable
        System.out.println(Arrays.toString(dogs));
    }
}
